<?php
include 'comite.php';
class integrantes extends CI_Controller {
	var $titp='Integrantes';
	var $url ='integrantes/';

	function index(){
		redirect($this->url.'filteredgrid');
	}

	function filteredgrid(){
		$this->load->library('rapyd');

		$filter = new datafilter_library();
		$filter->label = 'Buscar integrante';

		$sel=array('integrantes.id AS id','integrantes.nombre AS inombre','comite.nombre AS cnombre','integrantes.cargo');

		$filter->db->select($sel);
		$filter->db->from('integrantes');
		$filter->db->join('comite','integrantes.id_comite=comite.id');

		$filter->field('input','integrantes.nombre'  ,'Nombre')
			->attributes(array('style' => 'width:170px'));

		$filter->field('input','apellido'  ,'Apellido')
			->attributes(array('style' => 'width:170px'));

		$filter->field('input','cedula'  ,'Cedula')
			->attributes(array('style' => 'width:170px'));

		$filter->field('dropdown','cargo'  ,'Cargo')
			->options(array(
				'' =>'Todos',
				'C'=>'Coordinador',
				'M'=>'Miembro'))
			->attributes(array('style' => 'width:170px'));

		$filter->field('dropdown','id_comite','Comite')
			->option('','Todos')
			->options('SELECT id,nombre FROM comite ORDER BY nombre')
			->attributes(array('style'   => 'width: 200px'))
			->rule('trim|required');

		$filter->buttons('reset', 'search');
		$filter->build();

		$uri = anchor($this->url.'dataedit/show/<raencode><#id#></raencode>','<#id#>');

		$grid = new datagrid_library();
		$grid->label = 'Lista de Integrantes';
		$grid->per_page = 40;
		$grid->cid = '';
		$grid->source($filter);

		$grid->column('inombre','Nombre',true)->url('integrantes/dataedit/show/{id}');
		$grid->column('cnombre','Comite',true);
		$grid->column('cargo' ,'Cargo',true);

		$grid->add_button(array('url'=>$this->url.'dataedit/create'));
		$action = "javascript:window.location.href='".site_url('/')."'";
		$grid->button('btn_reg', 'Regresar', $action, 'TR');
		$grid->build();

		$data['content'] = $filter->output.$grid->output;
		$data['head']    = $this->rapyd->head().script('jquery.js');
		$data['title']   = '';
		$this->load->view('view_ventanas_sola', $data);

	}

	function dataedit(){
		$this->load->library('rapyd');

		$edit = new dataedit_library();
		$edit->label = 'Integrantes';
		$edit->back_url = site_url($this->url.'filteredgrid');

		$edit->source('integrantes');

		$edit->pre_process(array('insert','update'), array($this, 'pre_inte_inserup'));

		$edit->field('input','nombre'  ,'Nombre')
			->attributes(array('style' => 'width:170px'))
			->rule('trim|required|ucfirst');

		$edit->field('input','apellido','Apellido')
			->attributes(array('style' => 'width:170px'))
			->rule('trim|required|ucfirst');

		$edit->field('input','cedula'  ,'Cédula'  )
			->attributes(array('style' => 'width:170px'))
			->rule('trim|required');

		$edit->field('dropdown','sexo','Sexo')
			->options(array(
				'' =>'Seleccionar',
				'F'=>'Femenino',
				'M'=>'Masculino'))
			->rule('required');

		$edit->field('input','telefono'  ,'Teléfono')
			->attributes(array('style' => 'width:170px'));

		$edit->field('dropdown','cargo'  ,'Cargo')
			->options(array(
				'C'=>'Coordinador',
				'M'=>'Miembro'))
			->attributes(array('style' => 'width:170px'));

		$edit->field('dropdown','id_comite','Comite')
			->option('','Seleccionar')
			->options('SELECT id,nombre FROM comite ORDER BY nombre')
			->attributes(array('style'   => 'width: 200px'))
			->rule('trim|required');

		$edit->field('textarea', 'direccion','Dirección' );

		$edit->field('dropdown','id_comite','Comite')
			->option('','Seleccionar')
			->options('SELECT id,nombre FROM comite ORDER BY nombre')
			->attributes(array('style' => 'width: 200px'))
			->rule('trim|required');

		$edit->field('input','telefono'  ,'Teléfono')
			->attributes(array('style' => 'width:170px'));
		$edit->field('dropdown','cargo'  ,'Cargo')
			->options(array(
				'C'=>'Coordinador',
				'M'=>'Miembro'))
			->attributes(array('style' => 'width:170px'));

		$edit->field('textarea', 'direccion','Dirección' );


		$edit->buttons('modify','save','undo','back');
		$edit->build();

		$data['head']    = $this->rapyd->head();
		$data['title']   = '<h1>Ficha de Integrantes</h1>';
		$data['content'] = $edit;
		$this->load->view('view_ventanas_sola', $data);
	}

	function pre_inte_inserup($model){
		$cargo       = $model->get('cargo');
		$id_compania = $model->get('id_comite');
		//$id          = $model->pk['id'];

		if($cargo=='C'){
			$data = array( 'cargo' => 'M' );

			$this->db->where('id_comite'   , $id_compania);
			$this->db->where('cargo'       , $cargo);
			//$this->db->where('id <>'       , $id);
			$this->db->update('integrantes', $data);
		}
		return true;
	}

}
